package com.xiaoyu.commonlib.ui.base;

import android.Manifest;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.techfansy.utils.PermissionUtils;
import com.xiaoyu.commonlib.BillionCardConstants;
import com.xiaoyu.commonlib.R;
import com.xiaoyu.commonlib.network.BaseNetworkManager;
import com.xiaoyu.commonlib.ui.dialog.LoadingDialog;
import com.xiaoyu.commonlib.utils.StatusBarUtil;
import com.xiaoyu.commonlib.utils.UIUtils;

import java.lang.reflect.Field;

/**
 * activity 基类
 * Created by jk on 2018/9/28.
 */
public class BaseActivity extends AppCompatActivity implements LoadingDialog.EmptyDialogDismissListener {
    private static final int ACCESS_FINE_LOCATION_COMMANDS_REQUEST_CODE = 1;
    protected LinearLayout mRootView;
    protected View mStatusBarOccupyView; //状态栏占位view

    private LoadingDialog mLoadingDialog = null; //透明遮罩的加载框
    protected PermissionUtils permissionUtils;//相机权限工具类


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }

        //透明状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else {
            getWindow().addFlags(0x04000000);
        }


        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            //申请WRITE_EXTERNAL_STORAGE权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, ACCESS_FINE_LOCATION_COMMANDS_REQUEST_CODE);
        } else {

        }
        permissionUtils = new PermissionUtils();
        mLoadingDialog = new LoadingDialog(this, true);
        mLoadingDialog.setEmptyDismissListener(this);

        registerBroadcastReceiver();


    }

    @Override
    public void setContentView(int layoutResID) {
        StatusBarUtil.StatusBarLightMode(this);
        mRootView = new LinearLayout(this);
        mRootView.setOrientation(LinearLayout.VERTICAL);
        mRootView.setBackgroundColor(Color.WHITE);
        if (Build.VERSION.SDK_INT >= 19) {
            mStatusBarOccupyView = new View(this);
            mRootView.addView(mStatusBarOccupyView, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, UIUtils.getStatusBarHeight(this)));
        }

        View view = LayoutInflater.from(this).inflate(layoutResID, mRootView, false);
        mRootView.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        super.setContentView(mRootView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

    }


    /**
     * 设置占位view颜色为默认的渐变色(颜色为17用于我的页面)
     */
    protected void setOccupyViewLinearGradient() {
        if (mStatusBarOccupyView != null) {
            mStatusBarOccupyView.setBackgroundResource(R.drawable.bg_common_title);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        permissionUtils.setRequestCode(requestCode, permissions, grantResults);
    }

    //颜色为白色用于其他大多页面
    protected void setOccupyViewLinearGradientLogin() {
        if (mStatusBarOccupyView != null) {
            mStatusBarOccupyView.setBackgroundResource(R.drawable.bg_common_write);
        }
    }

    //颜色为27用于我的会员页面
    protected void setOccupyViewLinearGradientBlue() {
        if (mStatusBarOccupyView != null) {
            mStatusBarOccupyView.setBackgroundResource(R.drawable.bg_common_title_blue);
        }
    }

    protected void setOccupyViewLinearTransparencyLogin() {
        if (mStatusBarOccupyView != null) {
            mStatusBarOccupyView.setBackgroundColor(Color.TRANSPARENT);
        }
    }


    protected void hideOccupyView() {
        if (mStatusBarOccupyView != null) {
            mStatusBarOccupyView.setVisibility(View.GONE);
        }
    }

    protected void ShowOccupyView() {
        if (mStatusBarOccupyView != null) {
            mStatusBarOccupyView.setVisibility(View.VISIBLE);
        }
    }

    protected void showLoading() {
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    protected void dissmissLoading() {
        if (mLoadingDialog.isShowing()) {
            mLoadingDialog.dismissLoadingDialog();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dissmissLoading();
        unregisterBroadcastReceiver();
    }

    private void registerBroadcastReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BillionCardConstants.MSG_LOG_OUT);
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(mReceiver, intentFilter);
    }

    private void unregisterBroadcastReceiver() {
        try {
            unregisterReceiver(mReceiver);
        } catch (Exception e) {

        }
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                if (BillionCardConstants.MSG_LOG_OUT.equals(intent.getAction())) {
                    handleLogOut();
                } else if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
                    //处理断网
//                    if (!NetWorkUtil.isNetworkAvailable(context)) {
//                        onNoInternet();
//                    }
                }
            }
        }
    };

    /**
     * 退出登录回调
     */
    protected void handleLogOut() {
        finish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }


    @Override
    public void onDismiss(int reason) {
        if (reason == LoadingDialog.DISMISS_REASON_BACK) {
            BaseNetworkManager.getInstance().cancelAll();
            finish();
        }
    }
}
